package com.woniuxy.creditsystem.loanmag.service.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.mapper.LoanMapper;
import com.woniuxy.creditsystem.loanmag.service.LoanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Transactional(readOnly = true)
@Service
public class LoanServiceimpl implements LoanService {
    @Resource
    private LoanMapper loanMapper;

    @Override
    public PageInfo<LoansInfo> findByQuery(LoanQuery loanQuery) {
        PageInfo<LoansInfo> pageInfo = PageHelper.startPage(loanQuery.getPageNum(), loanQuery.getLimit()).doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                loanMapper.findByQuery(loanQuery);
            }
        });
        return pageInfo;
    }

    @Override
    public Detail findByorderid(String orderid) {
        Detail detail = loanMapper.findByorderid(orderid);
        return detail;
    }

    @Override
    public List<LoansInfo> exportall(LoanQuery loanQuery) {
        List<LoansInfo> list = loanMapper.findByQuery(loanQuery);
        return list;
    }
    //保存还款订单
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void saveReturn(ReturnPlanInfo r) {
        loanMapper.saveR(r);
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void setStatus(String orderid,String status) {
        loanMapper.setStstus(orderid,status);
    }

    @Override
    public List<UserInfo> findusers() {
        List<UserInfo> list = loanMapper.findusers();
        return list;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void savaappoint(CollectionInfo collectionInfo) {
        loanMapper.savacol(collectionInfo);
    }

    @Override
    public List<ReturnPlanInfo> findplanbyuid(int uid) {
        List<ReturnPlanInfo> list = loanMapper.findPlanByuid(uid);
        return list;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void changeRe(ReturnPlanInfo returnPlanInfo) {
        loanMapper.changeRe(returnPlanInfo);
    }

    @Override
    public void saveExcel() {

    }

    @Override
    public Boolean isClean(long uid) {
        Boolean status = true;
        List<ReturnPlanInfo> planInfos = loanMapper.findPlanByuid((int) uid);
        for (int i = 0; i < planInfos.size(); i++) {
            System.out.println(planInfos.get(i).getAlsoStatus());
            if (!planInfos.get(i).getAlsoStatus().equals("已还款")){
                status=false;
                break;
            }
        }
        System.out.println(status);
        return status;
    }

    @Override
    public List<ReturnPlanInfo> findAllPlans() {
        List<ReturnPlanInfo> allPlans = loanMapper.findAllPlans();
        return allPlans;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void setPlan(ReturnPlanInfo returnPlanInfo) {
        loanMapper.setPlan(returnPlanInfo);
    }

    @Override
    public int getMaxdaysbyOid(String onum) {
        int i = loanMapper.getmaxdays(onum);
        return i;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void setOrder(OrderInfo order) {
        loanMapper.setOrder(order);
    }
}
